<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
			<el-form-item label="问题简述" prop="issueName">
				<el-input v-model="dataForm.issueName" placeholder="问题简述"></el-input>
			</el-form-item>
			<el-form-item label="详细说明" prop="issueComment">
				<!-- <el-input v-model="dataForm.issueComment" placeholder="详细说明"></el-input> -->
				<el-input v-model="dataForm.issueComment" type="textarea" rows="10" placeholder="模板描述" />
			</el-form-item>
			<el-form-item label="紧急程度" prop="urgentLevel">
				<el-select v-model="dataForm.urgentLevel" placeholder="请选择">
					<el-option value="0" label="完全不紧急"></el-option>
					<el-option value="1" label="不紧急"></el-option>
					<el-option value="2" label="略紧急"></el-option>
					<el-option value="3" label="比较紧急"></el-option>
					<el-option value="4" label="非常紧急"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="是否解决" prop="resolveStatus">
				<el-radio-group v-model="dataForm.resolveStatus">
					<el-radio :label="0">未解决</el-radio>
					<el-radio :label="1">已解决</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useGenCodeIssueApi, useGenCodeIssueSubmitApi } from '@/api/genCodeIssue'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	issueName: '',
	issueComment: '',
	urgentLevel: '',
	resolveStatus: '0',
	createTime: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getGenCodeIssue(id)
	}
}

const getGenCodeIssue = (id: number) => {
	useGenCodeIssueApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	issueName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	issueComment: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	urgentLevel: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	resolveStatus: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useGenCodeIssueSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
